package com.bl.action.customer;

import java.util.ArrayList;
import java.util.List;

import com.bl.action.Command;
import com.bl.service.BeanFactory;
import com.dao.Crud;

/**
 * @author ${user}
 */
public class BeanActions<T> {

    Crud<T> dao = null;
    BeanFactory<T> beanFactory = null;


    public BeanActions(final Crud<T> aDao, final BeanFactory<T> aBeanFactory) {
        dao = aDao;
        beanFactory = aBeanFactory;
    }


    public Command<Void> getDeleteBeansFromDbOneByOneAction(final List<T> aBeans) {
        return new Command<Void>() {
            @Override
            public Void execute() {
                for (final T bean : aBeans) {
                    dao.delete(bean);
                }
                return null;
            }
        };
    }


    public Command<Void> getInsertBeansToDbOneByOneAction(final List<T> aBeans) {
        return new Command<Void>() {
            @Override
            public Void execute() {
                for (final T bean : aBeans) {
                    dao.insert(bean);
                }
                return null;
            }
        };
    }


    public Command<Void> getInsertBeansUsingBatchesAction(final List<T> aBeans) {
        return new Command<Void>() {
            @Override
            public Void execute() {
                dao.insert(aBeans);
                return null;
            }
        };
    }


    public Command<Void> getDeleteBeansUsingBatchesAction(final List<T> aBeans) {
        return new Command<Void>() {
            @Override
            public Void execute() {
                dao.delete(aBeans);
                return null;
            }
        };
    }

    public Command<List<T>> getGenerateBeansAction(final int aNumberOfBeans) {
        return new Command<List<T>>() {
            @Override
            public List<T> execute() {
                final List<T> beans = new ArrayList<T>(aNumberOfBeans);
                for (int i = 0; i < aNumberOfBeans; i++) {
                    beans.add(beanFactory.createBean());
                }
                return beans;
            }
        };
    }

    public Command<T> getFindByIdAction(final Integer aId) {
        return new Command<T>() {
            @Override
            public T execute() {
                return dao.find(aId);
            }
        };
    }
}
